# _*_ coding ：utf-8 _*_
# @Time : 2023/9/20 14:18
# @Author : 计科1914-02-陈鸿宇
# @File : 3.4
# @Project : ttkbootstrap.py

stu = [{
    "name": "张三",
    "phone": "18950371837"
}, {
    "name": "李四",
    "phone": "18950371833"
}, {
    "name": "张三",
    "phone": "18950371838"}]

while True:
    print('欢迎使用个人通讯录系统 ')
    print('1：查询联系人')
    print('2：插入新的联系人')
    print('3：删除已有联系人')
    print('4：输出所有联系人信息')
    print('5：退出')

    i = int(input("请输入指令："))

    if 0 < i < 6:
        if i == 1:
            name = input("请输入联系人姓名:")
            print("=======================")
            num = 0
            for s in stu:
                if s["name"] == name:
                    num += 1
                    print("第" + str(num) + "位联系人！")
                    print("联系人姓名:" + s["name"])
                    print("联系人电话:" + s["phone"])
                    print("=======================")

            if num == 0:
                print("您输入的姓名不在通讯录中!")
                print("=======================")

        elif i == 2:
            print("=======================")
            stu.append({"name": input("请输入联系人姓名:"),
                        "phone": input("请输入联系人电话:")})
            print("添加成功！")
            print("=======================")
        elif i == 3:
            ind = []

            name = input("请输入联系人姓名:")
            print("=======================")

            for s in stu:
                if s["name"] == name:
                    ind.append(stu.index(s))

            if len(ind) == 0:
                print("您输入的姓名不在通讯录中!")
                print("=======================")
            elif len(ind) == 1:
                stu.pop(ind[0])
                print("删除成功！")
                print("=======================")
            else:
                for i in ind:
                    print("第" + str(ind.index(i) + 1) + "位联系人！")
                    print("联系人姓名:" + stu[i]["name"])
                    print("联系人电话:" + stu[i]["phone"])
                    print("=======================")

                num = input("要删除第几位联系人:")
                if 0 <= int(num) - 1 < len(ind):
                    stu.pop(ind[int(num) - 1])
                    print("删除成功！")
                else:
                    print("联系人不存在！")
                print("=======================")

        elif i == 4:
            print("=======================")
            num = 0
            for s in stu:
                num += 1
                print("第" + str(num) + "位联系人！")
                print("联系人姓名:" + s["name"])
                print("联系人电话:" + s["phone"])
                print("=======================")

        elif i == 5:
            break
    else:
        print("指令有误！")
